package de.dhe.dao;

import static org.junit.Assert.assertTrue;

import java.util.List;

import org.apache.log4j.BasicConfigurator;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import de.preiscrawler.AlleAnbieter;
import de.preisrobot.entity.CrawlerLinkUrl;
import de.preisrobot.persistence.dao.LinkDao;
import de.preisrobot.service.GrabberService;

public class LinkTestDao {
	
	@Test
	public void testFindLinksForAnbieter()
	{
		BasicConfigurator.configure();
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
		"classpath*:applicationContext.xml");

		LinkDao dao = (LinkDao) appContext.getBean("linkDao");
		List<CrawlerLinkUrl> findLinksForAnbieter = dao.findLinksForAnbieter(AlleAnbieter.AMAZON);
		assertTrue(findLinksForAnbieter != null);
		assertTrue(findLinksForAnbieter.size() >0);
		
		for (CrawlerLinkUrl amazonGrabUrl : findLinksForAnbieter) {
			System.out.println(amazonGrabUrl);
		}
	}
	
	@Test
	public void testFindLinksByUrl()
	{
		BasicConfigurator.configure();
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
		"classpath*:applicationContext.xml");

		LinkDao dao = (LinkDao) appContext.getBean("linkDao");
		String sourceUrlString ="http://www.amazon.de/s/ref=amb_link_157495247_3?ie=UTF8&rh=n%3A427957031%2Cp_4%3AAcer&pf_rd_m=A3JWKAKR8XB7XF&pf_rd_s=browse&pf_rd_r=1PSBK77S16MRQ024WA83&pf_rd_t=101&pf_rd_p=248882167&pf_rd_i=514699031#/ref=sr_pg_3?rh=n%3A340843031%2Cn%3A!340844031%2Cn%3A427957031%2Cp_4%3AAcer&page=3&ie=UTF8&qid=1312025423";
	 
		CrawlerLinkUrl findLinksForAnbieter = dao.findLinlByUrl(sourceUrlString);
		assertTrue(findLinksForAnbieter != null);
 
		
		 
			System.out.println(findLinksForAnbieter);
	}
	
	@Test
	public void testFindNonExistingLinksByUrl()
	{
		BasicConfigurator.configure();
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
		"classpath*:applicationContext.xml");

		LinkDao dao = (LinkDao) appContext.getBean("linkDao");
		String sourceUrlString ="http://www.amazon.de/DAHE#DHE25423";
		CrawlerLinkUrl findLinksForAnbieter = dao.findLinlByUrl(sourceUrlString);
		assertTrue(findLinksForAnbieter == null);
 
		
		 
			System.out.println(findLinksForAnbieter);
	}
	
	@Test
	public void testLinkChecker()
	{
		BasicConfigurator.configure();
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
		"classpath*:applicationContext.xml");

		LinkDao dao = (LinkDao) appContext.getBean("linkDao");
		GrabberService service = (GrabberService) appContext.getBean("amazonGrabberService"); 
		
		
		List<CrawlerLinkUrl> findLinksForAnbieter = dao.findLinksForAnbieter(AlleAnbieter.AMAZON);
		assertTrue(findLinksForAnbieter != null);
		assertTrue(findLinksForAnbieter.size() >0);
		
		for (CrawlerLinkUrl amazonGrabUrl : findLinksForAnbieter) {
			service.grapUrl(amazonGrabUrl);
		}
	}

}
